package com.yand.frame.controller.system.user;



import com.github.pagehelper.PageInfo;
import com.yand.frame.common.Result;
import com.yand.frame.dao.ActivityRoleMapper;
import com.yand.frame.dto.user.UserDto;
import com.yand.frame.entity.ActivityRole;
import com.yand.frame.entity.ActivityUser;
import com.yand.frame.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;




    /**
     * 获取所有的用户列表
     * @param model
     * @return
     */
    @RequestMapping("/userList")
    public String toPage(Model model, UserDto dto){

        PageInfo pageInfo = userService.getUserList(dto);
        model.addAttribute("pageInfo",pageInfo);


        return "user/user-list";
    }


    /**
     * 跳转用户添加页面
     * @return
     */
    @GetMapping("/add")
    public String add(Model model){

        //获取当前角色所有信息
        List<ActivityRole> roleList = userService.getAllRole();
        model.addAttribute("roleList",roleList);
        return "user/user-add";
    }


    /**
     * 添加用户
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result addUser(ActivityUser user){
        //声明一个返回对象

        userService.addUser(user);
        return new Result(0);
    }

    /**
     *
     * @param id
     * @return
     */
    @PostMapping("/del/{userId}")
    @ResponseBody
    public Result delUser(@PathVariable(value = "userId") String id){

        userService.deleteUser(Integer.parseInt(id));

        return new Result(0);
    }


    @GetMapping("/pwd")
    public String toPwdPage(){

        return "user/user-pwd";
    }


    /**
     * 修改密码
     * @return
     */
    @PostMapping("/pwd")
    @ResponseBody
    public Result toupdatePwd(String newPassword){


        userService.updatePassword(newPassword);

        return new Result(0);
    }



    /**
     * 校验旧密码
     * @param rPassword
     * @return
     */
    @PostMapping("/validateOldPass")
    @ResponseBody
    public Result validateOldPass(String rPassword){
        boolean flag = userService.validateOldPass(rPassword);
        if(!flag){
            return new Result(1,"旧密码错误");
        }
        return new Result(0);
    }






}
